package com.yndsoft.jc.ui.memberpage.setting;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yndsoft.jc.MainActivity;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.core.ActivitiesManager;
import com.yndsoft.jc.netutils.AppUtils;
import com.yndsoft.jc.netutils.NotifyObject;
import com.yndsoft.jc.supplier.utils.ACache;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.ui.memberpage.login.ChangePswActivity;
import com.yndsoft.jc.widget.LashouMultiDialogRound;

import butterknife.Bind;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hasee on 2017/12/5.
 */

public class SettingActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.base_title)
    TextView base_title;

    @Bind(R.id.rl_help_center)
    RelativeLayout rl_help_centerl;//帮助中心
    @Bind(R.id.rl_suggestions)
    RelativeLayout rl_suggestions;//意见反馈
    @Bind(R.id.rl_changePsw)
    RelativeLayout rl_changePsw;//修改密码
    @Bind(R.id.rl_clear_cache)
    RelativeLayout rl_clear_cache;//清除缓存
    @Bind(R.id.rl_about)
    RelativeLayout rl_about;//关于独角兽
    @Bind(R.id.caches)
    TextView caches;//缓存
    @Bind(R.id.logout_btn)
    Button logout_btn;//退出登录

    private LashouMultiDialogRound dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        setViews();
        setListener();
    }

    private void setViews() {
        base_title.setText(getResources().getString(R.string.setting));
    }

    private void setListener() {
        base_backBtn.setOnClickListener(this);
        rl_help_centerl.setOnClickListener(this);
        rl_suggestions.setOnClickListener(this);
        rl_changePsw.setOnClickListener(this);
        rl_clear_cache.setOnClickListener(this);
        rl_about.setOnClickListener(this);
        logout_btn.setOnClickListener(this);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    String message = (String) msg.obj;
                    ToastUtil.show(mContext, message);
                    break;
            }
        }
    };

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
            case R.id.rl_help_center:
                intent = new Intent(this, HelpCenterActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_suggestions:
                intent = new Intent(this, SuggestionsActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_changePsw:
                intent = new Intent(this, ChangePswActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_clear_cache:
                dialog = new LashouMultiDialogRound(
                        this,
                        R.style.LashouDialog_null,
                        "是否现在清除缓存?",
                        "取消",
                        "确定",
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                if (null != dialog) {
                                    dialog.dismiss();
                                }
                            }
                        },
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                AppUtils.clearAllCache(mContext, new NotifyObject() {
                                    @Override
                                    public void message(String msg) {
                                        Message message = Message.obtain();
                                        message.what = 0;
                                        message.obj = msg;
                                        handler.sendMessage(message);
                                    }
                                });
                                caches.setText("0M");
                                if (null != dialog) {
                                    dialog.dismiss();
                                }
                            }
                        });
                dialog.show();
                break;
            case R.id.rl_about:
                intent = new Intent(this, AboutUsActivity.class);
                intent.putExtra("aboudID", 10036);
                startActivity(intent);
                break;
            case R.id.logout_btn:
                dialog = new LashouMultiDialogRound(
                        this,
                        R.style.LashouDialog_null,
                        "确定要退出登录吗?",
                        "取消",
                        "确定",
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        },
                        new View.OnClickListener() {
                            //                            MainActivity mainActivity = (MainActivity) ActivitiesManager.getInstance().getSpecialActivity(MainActivity.class);
                            @Override
                            public void onClick(View v) {
                                mSession.setRole(0);
                                mSession.setLogin(false);
                                mSession.setUserInfo(null);
                                mSession.setSuppilerAdmin(null);
                                ACache aCache = ACache.get(mContext);
                                aCache.remove(Constants.SUPPLIER_ID);
                                aCache.remove(Constants.SUPPLIER_SID);
                                aCache.remove(Constants.SUPPLIER_TID);
                                setResult(RESULT_OK);
                                Intent intent = new Intent(mContext, MainActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                            }
                        });
                dialog.show();
                break;
        }
    }
}
